package com.yiren.backstage.currency.bean;

import android.content.Context;

import com.yiren.backstage.currency.utils.StringUtil;

import org.apache.commons.lang.StringUtils;

/**
 * 新全档数据
 */
public class GyroGearDataBean {

    private String[] data;
    /*** 主板X轴角度 滚转角 */
    private float x1;
    /*** 主板Y轴角度 俯仰角 */
    private float y1;
    /*** 主板Z轴角度 偏航角 */
    private float z1;
    /*** 副板X轴角度 滚转角 */
    private float x2;
    /*** 副板Y轴角度 俯仰角 */
    private float y2;
    /*** 副板Z轴角度 偏航角 */
    private float z2;

    private float ox;
    private float oy;
    private float oz;
    private float dx;
    private float dy;
    private float dz;

    private String line;

    public String getLine() {
        return line;
    }

    public void setLine(String line) {
        this.line = line;
    }

    public GyroGearDataBean(Context context) {
        data = new String[6];
        ox = -90;
        oy = oz = 0;
    }

    public void getGyroData(Context context) {
        String lines = GyroData.getLines();
        if (!StringUtils.isEmpty(lines)) {
            setData(lines);
        }
    }

    public void setData(String gryo) {
        if (gryo == null || gryo.length() == 0) return;
        setLine(gryo);
        System.out.println("hong +++++++ setData:" + gryo);
        try {
            if (gryo.startsWith("$GYRO:") || gryo.startsWith("$CZY4:")) {
                String[] data = gryo.substring(6).split(",");
                System.arraycopy(data, 0, this.data, 0, Math.min(this.data.length, data.length));

                x1 = StringUtil.getStringFloat(this.data[0]);
                y1 = StringUtil.getStringFloat(this.data[1]);
                z1 = StringUtil.getStringFloat(this.data[2]);
                x2 = StringUtil.getStringFloat(this.data[3]);
                y2 = StringUtil.getStringFloat(this.data[4]);
                z2 = StringUtil.getStringFloat(this.data[5]);

                dx = x1 + x2 - ox;
                dy = y1 + y2 - oy;
                dz = z1 - z2 - oz;
            } else if (gryo.startsWith("$<GYRO;")) {
                String[] data = gryo.split(";");

                this.data[0] = data[2];
                this.data[1] = data[3];
                this.data[2] = data[1];
                this.data[3] = data[4];
                this.data[4] = data[5];
                this.data[5] = "0";

                x1 = StringUtil.getStringFloat(this.data[0]);
                y1 = StringUtil.getStringFloat(this.data[1]);
                z1 = StringUtil.getStringFloat(this.data[2]);
                x2 = StringUtil.getStringFloat(this.data[3]);
                y2 = StringUtil.getStringFloat(this.data[4]);
                z2 = StringUtil.getStringFloat(this.data[5]);

                dx = x1 + x2 - ox;
                dy = y1 + y2 - oy;
                dz = z1 - z2 - oz;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void reset() {
        ox = x1 + x2;
        oy = y1 + y2;
        oz = z1 - z2;
    }

    public void reset(float x, float y, float z) {
        ox = x;
        oy = y;
        oz = z;
    }

    public String getData(int index) {
        if (0 <= index && index < data.length) {
            return data[index];
        } else {
            return null;
        }
    }

    public float getX1() {
        return x1;
    }

    public float getY1() {
        return y1;
    }

    public float getZ1() {
        return z1;
    }

    public float getX2() {
        return x2;
    }

    public float getY2() {
        return y2;
    }

    public float getZ2() {
        return z2;
    }

    public float getOx() {
        return ox;
    }

    public float getOy() {
        return oy;
    }

    public float getOz() {
        return oz;
    }

    public float getDx() {
        return dx;
    }

    public float getDy() {
        return dy;
    }

    public float getDz() {
        return dz;
    }
}
